package com.pony.iphone.dto;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author pony
 * @version 1.0
 * @description: 获取题库的分页信息
 * @create 2025/9/19 11:05
 */
@Data
public class QuestionPageDto {
    // 题库id
    private Long libraryId;
    // 题库名称
    private String libraryName;
    // 题目id
    private Long questionId;
    // 题目
    private String question;
    // 问题答案，以及答案对应的分数
    @TableField(typeHandler = JacksonTypeHandler.class)
    private HashMap<String, Long> answerMap;

    @TableField(exist = false)
    private List<AnswerOption> answerList;

    // 内部类，转化 answerList, 计算出每一个题目被点击的概率百分比
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class AnswerOption {
        // 答案
        private String answer;
        // 答案分数
        private Long score;
        // 概率百分比
        private String percentage;
    }

    // answerList，数据库存储的json，刚查出来do映射的是map，需要转换成list
//    public void convertAnswerMapToList() {
//        if (this.answerMap != null && !this.answerMap.isEmpty()) {
//            // 计算总分
//            long totalScore = this.answerMap.values().stream().mapToLong(Long::longValue).sum();
//
//            this.answerList = this.answerMap.entrySet().stream()
//                    .map(entry -> {
//                        String answer = entry.getKey();
//                        Long score = entry.getValue();
//                        // 计算百分比
//                        double percentage = totalScore > 0 ? (score * 100.0) / totalScore : 0;
//                        return new AnswerOption(answer, score, String.format("%.2f%%", percentage));
//                    })
//                    .collect(Collectors.toList());
//        } else {
//            this.answerList = Collections.emptyList();
//        }
//    }

    // 权重比率
    public void convertAnswerMapToList() {
        if (this.answerMap != null && !this.answerMap.isEmpty()) {
            // 计算最大分数
            long maxScore = this.answerMap.values().stream()
                    .mapToLong(Long::longValue)
                    .max()
                    .orElse(0L);

            // 计算总权重
            long totalWeight = this.answerMap.values().stream()
                    .mapToLong(score -> maxScore + 1 - score)
                    .sum();

            this.answerList = this.answerMap.entrySet().stream()
                    .map(entry -> {
                        String answer = entry.getKey();
                        Long score = entry.getValue();
                        long weight = maxScore + 1 - score;
                        // 使用权重计算百分比
                        double percentage = totalWeight > 0 ? (double) weight / totalWeight * 100 : 0;
                        return new AnswerOption(answer, score, String.format("%.2f%%", percentage));
                    })
                    .collect(Collectors.toList());
        } else {
            this.answerList = Collections.emptyList();
        }
    }
}
